_functools.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import types
  2. import functools
  3. # from jaraco.functools 3.3
  4. def method_cache(method, cache_wrapper=None):
  5. """
  6. Wrap lru_cache to support storing the cache data in the object instances.
  7. Abstracts the common paradigm where the method explicitly saves an
  8. underscore-prefixed protected property on first call and returns that
  9. subsequently.
  10. >>> class MyClass:
  11. ... calls = 0
  12. ...
  13. ... @method_cache
  14. ... def method(self, value):
  15. ... self.calls += 1
  16. ... return value
  17. >>> a = MyClass()
  18. >>> a.method(3)
  19. 3
  20. >>> for x in range(75):
  21. ... res = a.method(x)
  22. >>> a.calls
  23. 75
  24. Note that the apparent behavior will be exactly like that of lru_cache
  25. except that the cache is stored on each instance, so values in one
  26. instance will not flush values from another, and when an instance is
  27. deleted, so are the cached values for that instance.
  28. >>> b = MyClass()
  29. >>> for x in range(35):
  30. ... res = b.method(x)
  31. >>> b.calls
  32. 35
  33. >>> a.method(0)
  34. 0
  35. >>> a.calls
  36. 75
  37. Note that if method had been decorated with ``functools.lru_cache()``,
  38. a.calls would have been 76 (due to the cached value of 0 having been
  39. flushed by the 'b' instance).
  40. Clear the cache with ``.cache_clear()``
  41. >>> a.method.cache_clear()
  42. Same for a method that hasn't yet been called.
  43. >>> c = MyClass()
  44. >>> c.method.cache_clear()
  45. Another cache wrapper may be supplied:
  46. >>> cache = functools.lru_cache(maxsize=2)
  47. >>> MyClass.method2 = method_cache(lambda self: 3, cache_wrapper=cache)
  48. >>> a = MyClass()
  49. >>> a.method2()
  50. 3
  51. Caution - do not subsequently wrap the method with another decorator, such
  52. as ``@property``, which changes the semantics of the function.
  53. See also
  54. http://code.activestate.com/recipes/577452-a-memoize-decorator-for-instance-methods/
  55. for another implementation and additional justification.
  56. """
  57. cache_wrapper = cache_wrapper or functools.lru_cache()
  58. def wrapper(self, *args, **kwargs):
  59. # it's the first call, replace the method with a cached, bound method
  60. bound_method = types.MethodType(method, self)
  61. cached_method = cache_wrapper(bound_method)
  62. setattr(self, method.__name__, cached_method)
  63. return cached_method(*args, **kwargs)
  64. # Support cache clear even before cache has been created.
  65. wrapper.cache_clear = lambda: None
  66. return wrapper
  67. # From jaraco.functools 3.3
  68. def pass_none(func):
  69. """
  70. Wrap func so it's not called if its first param is None
  71. >>> print_text = pass_none(print)
  72. >>> print_text('text')
  73. text
  74. >>> print_text(None)
  75. """
  76. @functools.wraps(func)
  77. def wrapper(param, *args, **kwargs):
  78. if param is not None:
  79. return func(param, *args, **kwargs)
  80. return wrapper